<?php
// +----------------------------------------------------------------------
// | 新版填写订单
// +----------------------------------------------------------------------
// | TongBaoChun <501468940@qq.com>
// +----------------------------------------------------------------------
// | 2015/10/29
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';
include_once '/var/www/html/new/d/module/link_product/function.php';

if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"请先登录后再操作","code":0}');


$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$inside  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));



$pid = $_REQUEST['pid']+0; // 产品ID
$aid = $_REQUEST['aid']+0; // 供应商ID
if($pid==0 || $aid==0) exit('{"status":"fail","msg":"参数错误"}');


// 获取产品信息
$sql = "select l.id as lid,t.id as tid,p.id as pid,p.apply_did,l.title as ltitle,t.title as ttitle,t.tprice,l.p_type,t.buy_limit_low,t.buy_limit_up,t.ddays,f.dhour,t.order_start,t.order_end,f.tourist_info,t.overdue_refund,t.delaydays ,t.pay,t.reb,t.reb_type from uu_products p left join uu_land l on p.contact_id=l.id left join uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id where p.p_status=0 and p.apply_limit=1 and p.id=$pid limit 1";
$GLOBALS['le']->query($sql);
if(!($p_info = $GLOBALS['le']->fetch_assoc())) exit('{"status":"fail","msg":"产品未通过审核"}');
$p_info['htmlValid'] = htmlValid($p_info);

$self_supply = ($p_info['apply_did'] == $_SESSION['sid']) ? true:false;


// 购买权限检测
if($self_supply==false)
{
	if($p_info['apply_did'] == $aid)
	{
		$sql = "select pids from pft_product_sale_list where aid=$aid and fid={$_SESSION['sid']} limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc()) exit('{"status":"fail","msg":"数据出错"}');
		if($GLOBALS['le']->f('status')!=0) exit('{"status":"fail","msg":"无权限购买"}');
		$pids_a = explode(',', $GLOBALS['le']->f('pids'));
		if($pids_a[0]!='A' && !in_array($pid, $pids_a)) exit('{"status":"fail","msg":"无权限购买"}');
	}
	else
	{
		$sql = "select id,pid,sid,active,lvl,status from pft_p_apply_evolute where fid={$_SESSION['sid']} and status=0 sid=$aid limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc()) exit('{"status":"fail","msg":"无权限购买"}');
		if($GLOBALS['le']->f('status')==1) exit('{"status":"fail","msg":"无权限购买"}');
	}
}





if($p_info['p_type']=='F')// 套票
{
	include_once("/var/www/html/new/d/class/PackTicket.class.php");
	$lid = $p_info['lid'];
	$pack = new PackTicket($lid);
	$useDate = $pack->useDate();
	$mintime = substr($useDate['sDate'],0, 10);// 有效开始时间
	$maxtime = $useDate['mDate'] ? $useDate['mDate']:'2020-10-10';// 有效结束时间
	$begintime = (substr($useDate['sDate'],0,10) > substr($useDate['eDate'],0,10)) ? -1:$mintime;//
	if($begintime==-1) exit('{"status":"fail","msg":"无有效出行日期，请更换产品"}');
	$PackChildTicket = array();// 获取关联套票
	foreach($pack->attribute as $row_1){
		foreach($pack->childTicketDatas as $row_2){
			if($row_1['pid']!=$row_2['id']) continue;
			$PackChildTicket[] = array( 'p_num'  => $row_1['num'], 'p_name' => $row_2['p_name']);
			break;
		}
	}
}
else
{
	// 计算最小预定时间
	$ddays = $p_info['ddays'];
	$dhour = $p_info['dhour'];
	if(strtotime(date('Y-m-d H:i:s')) > strtotime(date('Y-m-d '.$dhour))) $ddays += 1;
	$begintime = date("Y-m-d", time()+3600*24*$ddays);
}

if($begintime < date('Y-m-d')) $begintime = date('Y-m-d');
$pStorage = PFTCoreAPI::pStorage($inside, $_SESSION['saccount'], $p_info['pid'], $p_info['sapply_did'], $begintime);
if($pStorage['js']['p']==-1) exit('{"status":"fail","msg":"无有效出行日期，请更换产品"}');

$p_info['js'] = $pStorage['js']['p'];
$p_info['ls'] = $pStorage['ls']['p'];
$p_info['stroage'] = $pStorage['js']['s'];


// 获取其他门票
if($p_info['p_type']!='C' || $p_info['p_type']!='H' || $p_info['p_type']!='F')
{
	$lid = $p_info['lid'];
	$sql = "select l.id as lid,t.id as tid,p.id as pid,p.apply_did,l.title as ltitle,t.title as ttitle,t.tprice,l.p_type,t.buy_limit_low,t.buy_limit_up,t.ddays,f.dhour,t.order_start,t.order_end,f.tourist_info,t.overdue_refund,t.delaydays ,t.pay,t.reb,t.reb_type from uu_products p left join uu_land l on p.contact_id=l.id left join uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id where p.p_status=0 and p.apply_limit=1 and l.id=$lid";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $landTickets[$row['pid']] = $row;

	if(count($landTickets))
	{
		foreach($landTickets as $row)
		{

			// 属性不一致无法定联票
			if($row['ddays']!=$p_info['ddays']) continue;
			if($row['order_end']!=$p_info['order_end']) continue;
			if($row['delaydays']!=$p_info['delaydays']) continue;
			if($row['order_limit']!=$p_info['order_limit']) continue;
			if($row['order_start']!=$p_info['order_start']) continue;
			if($row['tourist_info']!=$p_info['tourist_info']) continue;
			if($row['overdue_refund']!=$p_info['overdue_refund']) continue;
			if($row['pid']!=$pid)
			{
				$pStorage = PFTCoreAPI::pStorage($inside, $_SESSION['saccount'], $row['pid'], $aid, $begintime);
				if($pStorage['js']['p']==-1) continue;
				$row['js'] = $pStorage['js']['p'];
				$row['ls'] = $pStorage['ls']['p'];
				$row['stroage']   = $pStorage['js']['s'];
				$row['htmlValid'] = htmlValid($p_info);
				
			}else $row = $p_info;

			$saleTickets[$row['pid']] = $row;
		}
	}
}



// 获取常用联系人
$contacts = array();
$sql = "select cinfos from pft_member_extinfo where fid={$_SESSION['sid']} limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
if(($cinfos_s = $GLOBALS['le']->f("cinfos")))
{
	$cinfos_a = explode('|', $cinfos_s);
	krsort($cinfos_a);
	
	$nekeys = array('name', 'tel', 'idCard');
	foreach($cinfos_a as $row)
	{
		$info = explode(':', $row);
		if(!isset($info[2])) $info[2] = '';
		$contacts[] = array_combine($nekeys, $info);
	}
}

// 账号资金情况

$capital = array();

$resxml1 = $inside->PFT_Member_Fund($_SESSION['sid'],0);
$simxml1 = simplexml_load_string($resxml1);
$capital['balance'] = $simxml1->Rec->UUamoney/100;// 账户余额

$resxml2 = $inside->PFT_Member_Fund($_SESSION['sid'], 1, $aid);
$simxml2 = simplexml_load_string($resxml2);
$capital['credit1'] = (int)$simxml2->Rec->UUkmoney/100;// 可用额度

$resxml3 = $inside->PFT_Member_Fund($_SESSION['sid'], 2, $aid);
$simxml3 = simplexml_load_string($resxml2);
$capital['credit2'] = (int)$simxml2->Rec->UUbasecredit/100;// 授信额度
$capital['credit3'] = $capital['credit1'] + $capital['credit2'];

// 整合数据发送前端
$response = array(
	'status'   => 'success',
	'capital'  => $capital,
	'contacts' => $contacts,
	'land' => array(
		'lid'       => $p_info['lid'],
		'pay'       => $p_info['pay'],
		'ltitle'    => $p_info['ltitle'],
		'p_type'    => $p_info['p_type'],
		'memberSID' => $_SESSION['sid'],
		'begintime' => $begintime,
		'tickets'   => $saleTickets,
		
	),
);


print_r('<pre/>');
print_r($response);
// exit(json_encode($response));















function chooseData(){
	return array(
		"pid"       => $pid,
		"id"        => "purchaseNum",
		"name"      => "tnum",
		"ttitle"    => $p_info['ttitle'].($p_info['v_time_limit']==0 ? '' : '(<span style="color:#F33">验票时间'.str_replace('|', '至', $p_info['v_time_limit']).'</span>)'),
		"buynum"    => $buynum,
		"jsprice"   => $jsprice,
		"uprice"    => $uprice,
		"tprice"    => $p_info['tprice'],
		"minTime"   => $begintime,
		"ddays"     => $p_info['ddays'],
		"buy_limit_low" => $p_info['buy_limit_low'],
		'buy_limit_up'  => $p_info['buy_limit_up'],
		'pay'       =>$p_info['pay'],
		'sendVoucher'=>$p_info['sendVoucher'],
		'order_start'=>$p_info['order_start'],
		'order_end'=>$p_info['order_end'],
		'reb'=>$p_info['reb'],
		'reb_type'=>$p_info['reb_type'],
		'storage'=>$storage,
	);
}


function htmlValid($p_info){
	$valid = '';
	if($p_info['delaydays']>0){
		if($p_info['delaytype']==0) $valid = '游玩日期(含)'.$p_info['delaydays'].'天内有效';
		if($p_info['delaytype']==1) $valid = '下单日期(含)'.$p_info['delaydays'].'天内有效';
	}
	if($p_info['order_start']=='' && $p_info['delaydays'] == 0) $valid = '订单当天有效';
	
	if($p_info['order_start']!='' && $p_info['order_end']!='' ){
		$valid = date('Y-m-d', strtotime($p_info['order_start'])).' 至 '.date('Y-m-d', strtotime($p_info['order_end']));
	}elseif($p_info['order_start']=='' && $p_info['order_end']!=''){
		$valid = '截止'.$p_info['order_end'];
	}
	return $valid;
}